package com.zhangbaiyan.test5;

import java.util.ArrayList;
import java.util.Collections;
import java.util.concurrent.Callable;

/**
 * @author zhangbaiyan
 * @date 2024-09-24 21:57
 * @description:
 * @version:
 */
public class MyCallable implements Callable<Integer> {
    ArrayList<Integer> list;

    public MyCallable(ArrayList<Integer> list) {
        this.list = list;
    }

    @Override
    public Integer call() throws Exception {
        ArrayList<Integer> boxList = new ArrayList<>();
        //1.循环
        //2.同步代码块
        //3.判断
        //4.判断
        while (true) {
            synchronized (MyCallable.class) {
                if (list.size() == 0) {
                    System.out.println(Thread.currentThread().getName() + boxList);
                    break;
                } else {
                    //继续抽奖
                    Collections.shuffle(list);
                    int prize = list.remove(0);
                    boxList.add(prize);
//                    System.out.println(getName()+"拿到"+prize+"元大奖");
                }

            }

                Thread.sleep(100);

        }
        //把集合的最大值返回
        if (boxList.size() ==0){
            return null;
        }else {
            return Collections.max(boxList);
        }

    }

}

